<?php
namespace app\entries\controller;
use think\Controller;
use think\Loader;

// +************
// | 词条管理
// +************
class Entries extends Logincheck
{

/***************************匠人部分*************************************/

    //查看匠人信息页面
    function jiangren($page=1, $limit=10)
    {
        $currentPage = $this->MaxPage('Entries', 'jiangren', $limit, $page);
        $retval = model("Entries")->showjr($currentPage, $limit);
        $this->assign("page",$currentPage);
        $this->assign("data",$retval);
        return $this->fetch();
    }

    //增加匠人信息页面
    function addjiangren()
    {
        return $this->fetch();
    }

    //增加匠人信息
    function addjr($name, $score, $teacase, $teaarea, $bangdan, $briefintro, $contact, $phone, $wechat, $detailintro)
    {
        $data = [
            "name" => $name,
            "score" => $score,
            "teacase_id" => $teacase,
            "teaarea_id" => $teaarea,
            "bangdan_id" => $bangdan,
            "briefintro" => $briefintro,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "addtime" => time()
        ];
        $retval = model('Entries')->addjr($data);
        if($retval == 1){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }
    }

    //修改匠人信息页面
    function modifyjr($id)
    {
        $retval = model("Entries")->modifyjr($id);
        $this->assign("data", $retval);
        return $this->fetch();
    }

    //修改匠人信息
    function modifyjrafter($id, $name, $score, $teacase, $teaarea, $bangdan, $briefintro, $contact, $phone, $wechat, $detailintro)
    {
        $data = [
            "name" => $name,
            "score" => $score,
            "teacase_id" => $teacase,
            "teaarea_id" => $teaarea,
            "bangdan_id" => $bangdan,
            "briefintro" => $briefintro,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "updatetime" => time()
        ];
        $retval = model('Entries')->updatejr($id, $data);
        if($retval == 1){
            return $this->success("修改成功","jiangren");
        }else{
            return $this->error("修改出错，请重新修改");
        }
    }

    //删除匠人信息
    function deljr($id)
    {
        $retval = model("Entries")->deljr($id);
        if($retval != 1){
            return $this->error("删除失败");
        }else{
            return $this->success("删除成功", "jiangren");
        }

    }

    //从excel导入数据
    function inserExcel()
    {
        if(request() -> isPost())
        {
            Loader::import('PHPExcel.Classes.PHPExcel');
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
            Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');
            //获取表单上传文件
            $file = request()->file('excel');
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public');  //上传验证后缀名,以及上传之后移动的地址  \public 目录下

            if($info)
            {
                //echo $info->getFilename();
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . $exclePath;//上传文件的地址
                $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

                $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $data = [];
                foreach($excel_array as $k=>$v) {
                        $data[$k]['teacase'] = $v[1];
                        $data[$k]['teaarea'] = $v[2];
                        $data[$k]['bangdan'] = $v[3];
                        $data[$k]['name'] = $v[4];
                        $data[$k]['score'] = $v[5];
                        $data[$k]['briefintro'] = $v[6];
                        $data[$k]['contact'] = $v[7];
                        $data[$k]['phone'] = $v[8];
                        $data[$k]['wechat'] = $v[9];
                        $data[$k]['detailintro'] = $v[10];
                 }
                 print_pre($data);
                 $retval = model("Entries")->insertExcel($data);
                 if($retval==1){
                    return $this->success("上传数据成功");
                 }else{
                    return $this->error("上传数据出错");
                 }

            }else
            {
                echo $file->getError();
            }
        }else
        {
            return $this -> fetch();
        }
    }

    //从Excel导入匠人页面
    function fromExceljr()
    {
        return $this->fetch();
    }


/*************************品牌部分***********************************/

    //查看品牌信息
    function pinpai($page=1, $limit=10)
    {
        $currentPage = $this->MaxPage('Entries', 'pinpai', $limit, $page);
        $retval = model("Entries")->showpp($currentPage, $limit);
        $this->assign("page",$currentPage);
        $this->assign("data",$retval);
        return $this->fetch();
    }

    //增加品牌信息页面
    function addpinpai()
    {
        return $this->fetch();
    }

    //手动增加品牌信息
    function addpp($name, $relatedwords, $teacase, $teaarea, $bangdan, $sale, $company, $pnumber, $contact, $phone, $wechat, $detailintro)
    {
        $data = [
            "name" => $name,
            "relatedwords" => $relatedwords,
            "teacase_id" => $teacase,
            "teaarea_id" => $teaarea,
            "bangdan_id" => $bangdan,
            "sale" => $sale,
            "company" => $company,
            "pnumber" => $pnumber,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "addtime" => time()
        ];
        $retval = model('Entries')->addpp($data);
        if($retval == 1){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }

    }

    //修改品牌信息页面
    function modifypp($id)
    {
        $retval = model("Entries")->modifypp($id);
        $this->assign("data", $retval);
        return $this->fetch();
    }

    //修改品牌信息
    function modifyppafter($id,$name, $relatedwords, $teacase, $teaarea, $bangdan, $sale, $company, $pnumber, $contact, $phone, $wechat, $detailintro)
    {
        $data = [
            "name" => $name,
            "relatedwords" => $relatedwords,
            "teacase_id" => $teacase,
            "teaarea_id" => $teaarea,
            "bangdan_id" => $bangdan,
            "sale" => $sale,
            "company" => $company,
            "pnumber" => $pnumber,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "addtime" => time()
        ];
        $retval = model('Entries')->updatepp($id, $data);
        if($retval == 1){
            return $this->success("修改成功","pinpai");
        }else{
            return $this->error("修改失败");
        }
    }


    //删除品牌信息
    function delpp($id)
    {
        $retval = model("Entries")->delpp($id);
        if($retval != 1){
            return $this->error("删除失败");
        }else{
            return $this->success("删除成功", "pinpai");
        }

    }

    //从Excel导入品牌信息页面
    function fromExcelpp()
    {
        return $this->fetch();
    }

    //将Excel里的品牌信息导入数据库
    function insertExcelpp()
    {
         if(request() -> isPost())
        {
            Loader::import('PHPExcel.Classes.PHPExcel');
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
            Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');
            //获取表单上传文件
            $file = request()->file('excel');
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public');  //上传验证后缀名,以及上传之后移动的地址  \public 目录下

            if($info)
            {
                //echo $info->getFilename();
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . $exclePath;//上传文件的地址
                $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

                $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                $data = [];

                foreach($excel_array as $k=>$v) {
                    $data[$k]['teacase'] = $v[1];
                    $data[$k]['teaarea'] = $v[2];
                    $data[$k]['bangdan'] = $v[3];
                    $data[$k]['name'] = $v[4];
                    $data[$k]['relatedwords'] = $v[5];
                    $data[$k]['sale'] = $v[6];
                    $data[$k]['company'] = $v[7];
                    $data[$k]['pnumber'] = $v[8];
                    $data[$k]['contact'] = $v[9];
                    $data[$k]['phone'] = $v[10];
                    $data[$k]['wechat'] = $v[11];
                    $data[$k]['detailintro'] = $v[12];
                 }
                 $retval = model("Entries")->insertExcelpp($data);
                 if($retval==1){
                    return $this->success("上传数据成功");
                 }else{
                    return $this->error("上传数据出错");
                 }

            }else
            {
                echo $file->getError();
            }
        }else
        {
            return $this -> fetch();
        }
    }


/*****************************珍品榜*************************************/

    //查看珍品信息
    function zhenpin($page=1, $limit=10)
    {
        $currentPage = $this->MaxPage('Entries', 'zhenpin', $limit, $page);
        $retval = model("Entries")->showzp($currentPage, $limit);
        $this->assign("page",$currentPage);
        $this->assign("data",$retval);
        return $this->fetch();
    }

    //增加珍品信息页面
    function addzhenpin()
    {
        return $this->fetch();
    }

    //手动增加珍品信息
    function addzp($teacase, $teaarea, $bangdan, $name, $producer, $zs, $gs, $ct, $ds, $other, $price, $contact, $phone, $wechat, $detailintro)
    {
        $tosymbol=['是'=>'√', '否'=>'×']; //字符转换
        $zs = $tosymbol[$zs];
        $gs = $tosymbol[$gs];
        $ct = $tosymbol[$ct];
        $ds = $tosymbol[$ds];

        $data = [
            "teacase_id" => $teacase,
            "teaarea_id" => $teaarea,
            "bangdan_id" => $bangdan,
            "name" => $name,
            "producer" => $producer,
            "zs" => $zs,
            "gs" => $gs,
            "ct" => $ct,
            "ds" => $ds,
            "other" => $other,
            "price" => $price,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "addtime" => time()
        ];
        $retval = model('Entries')->addzp($data);
        if($retval == 1){
            return $this->success("添加成功");
        }else{
            return $this->error("添加失败");
        }

    }

    //修改真品信息页面
    function modifyzp($id)
    {
        $tosymbol=['√' => '是', '×' => '否']; //字符转换
        $retval = model("Entries")->modifyzp($id);
        $retval['zs'] = $tosymbol[$retval['zs']];
        $retval['gs'] = $tosymbol[$retval['gs']];
        $retval['ct'] = $tosymbol[$retval['ct']];
        $retval['ds'] = $tosymbol[$retval['ds']];
        // echo "<pre>";
        // print_r($retval );exit;
        $this->assign("data", $retval);
        return $this->fetch();
    }

    //修改真品信息
    function modifyzpafter($id, $teacase, $teaarea, $bangdan, $name, $producer, $zs, $gs, $ct, $ds, $other, $price, $contact, $phone, $wechat, $detailintro)
    {
        $tosymbol=['是'=>'√', '否'=>'×']; //字符转换
        $zs = $tosymbol[$zs];
        $gs = $tosymbol[$gs];
        $ct = $tosymbol[$ct];
        $ds = $tosymbol[$ds];

        $data = [
            "teacase_id" => $teacase,
            "teaarea_id" => $teaarea,
            "bangdan_id" => $bangdan,
            "name" => $name,
            "producer" => $producer,
            "zs" => $zs,
            "gs" => $gs,
            "ct" => $ct,
            "ds" => $ds,
            "other" => $other,
            "price" => $price,
            "contact" => $contact,
            "phone" => $phone,
            "wechat" => $wechat,
            "detailintro" => $detailintro,
            "updatetime" => time()
        ];

        $retval = model('Entries')->updatezp($id, $data);
        if($retval == 1){
            return $this->success("修改成功","zhenpin");
        }else{
            return $this->error("修改失败");
        }
    }

    //删除珍品信息
    function delzp($id)
    {
        $retval = model("Entries")->delzp($id);
        if($retval != 1){
            return $this->error("删除失败");
        }else{
            return $this->success("删除成功", "zhenpin");
        }

    }


    //从Excel导入珍品信息页面
    function fromExcelzp()
    {
        return $this->fetch();
    }

    //将Excel里的珍品信息导入数据库
    function insertExcelzp()
    {
         if(request() -> isPost())
        {
            Loader::import('PHPExcel.Classes.PHPExcel');
            Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
            Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');
            //获取表单上传文件
            $file = request()->file('excel');
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public');  //上传验证后缀名,以及上传之后移动的地址  \public 目录下

            if($info)
            {
                //echo $info->getFilename();
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . $exclePath;//上传文件的地址
                $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

                $excel_array=$obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
                array_shift($excel_array);  //删除第一个数组(标题);
                array_shift($excel_array);

                $data = [];
                foreach($excel_array as $k=>$v) {

                    $data[$k]['teacase'] = $v[1];
                    $data[$k]['teaarea'] = $v[2];
                    $data[$k]['bangdan'] = $v[3];
                    $data[$k]['name'] = $v[4];
                    $data[$k]['producer'] = $v[5];
                    $data[$k]['zs'] = $v[6];
                    $data[$k]['gs'] = $v[7];
                    $data[$k]['ct'] = $v[8];
                    $data[$k]['ds'] = $v[9];
                    $data[$k]['other'] = $v[10];
                    $v[11] = explode('¥',$v[11]);
                    $v[11] = end($v[11]);//去掉'¥'
                    $v[11] = explode(',',$v[11]);
                    $v[11] = implode("",$v[11]);//去掉","符号
                    $data[$k]['price'] = $v[11];
                    $data[$k]['contact'] = $v[12];
                    $data[$k]['phone'] = $v[13];
                    $data[$k]['wechat'] = $v[14];
                    $data[$k]['detailintro'] = $v[15];
                 }
                 $retval = model("Entries")->insertExcelzp($data);
                 if($retval==1){
                    return $this->success("上传数据成功");
                 }else{
                    return $this->error("上传数据出错");
                 }

            }else
            {
                echo $file->getError();
            }
        }else
        {
            return $this -> fetch();
        }
    }


    //页数
    function MaxPage($model, $table, $limit, $page)
    {
        $lim = $limit;
        $Num = model($model)->number($table);
        $maxPage = ceil($Num/$lim);
        if($page <= 1){
            $page = 1;
        }elseif($page>$maxPage){
            $page = $maxPage;
        }
        return $page;
    }

}


?>